using System;
using System.Configuration;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using Ninject.Web.Mvc;
using Pansoft.HIME.Base.Interfaces.Repositories;

namespace Pansoft.HIME.Manager
{
    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper _Bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof (OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof (NinjectHttpModule));
            _Bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            _Bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);

            //GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
//            var connectionString = ConfigurationManager.ConnectionStrings["MONGOHQ_URL"].ConnectionString;
//            var databaseName = ConfigurationManager.AppSettings["Database"];
//
//            kernel.Bind<IRepository>().To<MongoRepository>()
//                .WithConstructorArgument("connectionString", connectionString)
//                .WithConstructorArgument("databaseName", databaseName);
//
//            kernel.BindHttpFilter<TokenAuthorisationAttribute>(FilterScope.Global);
//
        }
    }
}